<?php

declare(strict_types=1);

namespace App\Model\PssLogModel;

use App\Model\BaseModel\BasePssLogModel;
class InterfaceRequest extends BasePssLogModel
{
    public $table = 'interface_log';
    
    /**
     * 日志明细map
     * @var unknown
     */
    public $logIntefaceLogMap = [
        'OrderCancel'           => '订单取消',
        'OrderAfterSale'        => '订单售后',
        'OrderPay'              => '订单支付',
        'CreateOrder'           => '订单创建',
        'OrderAfterSaleVerify'  => '订单售后二审'
    ];
    
    /**
     * 添加日志
     * @param array $param
     */
    public function addLog( $param = [] )
    {
        $interface_code = $param['interface_code'];
        $request_param  = $param['request_param'];
        $insert = [
            'interface_code'    => $interface_code,
            'bid'               => $param['bid'],
            'type'              => isset($param['type']) ? $param['type'] : 0 ,
            'request_ip'        => getIp(),
            'request_use_time'  => isset($param['request_use_time']) ? $param['request_use_time'] : '0.000',
            'request_param'     => $request_param,
            'response_data'     => $param['response_data'],
            'response_code'     => $param['response_code'],
            'response_msg'      => $param['response_msg'],
            'cd'                => time()
        ];
        $reqData = json_decode($request_param,true);
        
        $event_sn = 'other';
        if( isset($this->logIntefaceLogMap[$interface_code]) && !empty($reqData['requestData']) ) {
            $requestData = $reqData['requestData'];
            if( !empty($requestData['orderSn']) ) {
                $event_sn = $requestData['orderSn'];
            } elseif ( !empty($requestData['order_sn']) ) {
                $event_sn = $requestData['order_sn'];
            }
        }
        
        $insert['event_sn'] = $event_sn;
        return $this->add($insert);
    }
}
